package com.youyoulong.framework.controller;

import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.youyoulong.framework.util.ThreadVariableUtil;
import com.youyoulong.framework.vo.IndexVo;

@Controller
@RequestMapping(value = "/")
public class IndexController {

	@RequestMapping(value = "/index/{applicationName}")
	public ModelAndView goIndexPage(IndexVo indexVo, HttpServletRequest request) {
		request.setAttribute("applicationName", indexVo.getApplicationName());
		return new ModelAndView("base/index", "indexVo", indexVo);
	}

	@POST
	@RequestMapping(value = "/test/postdata/{justTest}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Map<String, Object> test(@PathVariable("justTest") String justTest,
			@RequestBody Map<String, Object> params) {
		HttpServletRequest request = (HttpServletRequest) ThreadVariableUtil.create()
				.getAttribute(ThreadVariableUtil._currentRequestKey);
		params.put("currDate", new Date(System.currentTimeMillis()));
		params.put("justTest", justTest);
		Enumeration<String> headers = request.getHeaderNames();
		while (headers.hasMoreElements()) {
			String name = headers.nextElement();
			params.put(name, request.getHeader(name));
		}
		return params;
	}
}
